/*
 * NetBiz.java
 * business
 *
 * Created by ChenTao on 2016/9/9.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.data.net;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.request.PostRequest;
import com.yidiandao.business.utils.OKHttpUtils;
import com.yidiandao.business.utils.StringUtils;
import com.yidiandao.business.utils.SysUtils;
import com.yidiandao.business.utils.UIUtils;

import java.io.File;
import java.util.Map;

/** 网络请求库. */
public class NetBiz {

    /** 取消请求 */
    public static void cancelRequest(String tag) {
        OKHttpUtils.cancelRequest(tag);
    }

    /** 发送一个Post网络请求. */
    public <T extends BaseResponse> void sendPostRequest(
            final BaseRequest request,
            final Class<T> responseClazz,
            final NetListener<T> netListener) {

        if (StringUtils.isEmpty(request.getUrl())) {
            throw new NullPointerException("Http Url can not be null !!!");
        } else if (request.getRequestMap() == null) {
            throw new NullPointerException("Request Map can not be null !!!");
        } else if (StringUtils.isEmpty(request.getTag())) {
            throw new NullPointerException("Tag can not be null !!!");
        }

        // 所有接口加上app_version
        request.setAppVersion(SysUtils.getAppVersionName(UIUtils.getContext()));

        // 注意：getRequestMap 与 getReqTokenString 的顺序
        OkGo.post(request.getUrl())
                .tag(request.getTag())
                .params(request.getRequestMap())
                .headers("reqtoken", request.getReqTokenString())
                .execute(new BaseCallBack<>(request, responseClazz, netListener));
    }

    /** 发送Get请求 */
    public <T extends BaseResponse> void sendGetRequest(
            final BaseRequest request,
            final Class<T> responseClazz,
            final NetListener<T> netListener) {
        if (StringUtils.isEmpty(request.getUrl())) {
            throw new NullPointerException("Http Url can not be null !!!");
        } else if (request.getRequestMap() == null) {
            throw new NullPointerException("Request Map can not be null !!!");
        } else if (StringUtils.isEmpty(request.getTag())) {
            throw new NullPointerException("Tag can not be null !!!");
        }

        // 所有接口加上app_version
        request.setAppVersion(SysUtils.getAppVersionName(UIUtils.getContext()));

        // 注意：getRequestMap 与 getReqTokenString 的顺序
        OkGo.get(request.getUrl())
                .tag(request.getTag())
                .params(request.getRequestMap())
                .headers("reqtoken", request.getReqTokenString())
                .execute(new BaseCallBack<>(request, responseClazz, netListener));
    }

    /** 上传多个文件的网络请求 */
    public <T extends BaseResponse> void uploadFiles(
            UploadFileRequest request,
            final Class<T> responseClazz,
            final NetListener<T> netListener) {

        if (StringUtils.isEmpty(request.getUrl())) {
            throw new NullPointerException("Http Url can not be null !!!");
        } else if (request.getRequestMap() == null) {
            throw new NullPointerException("Request Map can not be null !!!");
        } else if (StringUtils.isEmpty(request.getTag())) {
            throw new NullPointerException("Tag can not be null !!!");
        }

        // 所有接口加上app_version
        request.setAppVersion(SysUtils.getAppVersionName(UIUtils.getContext()));

        // 注意：getRequestMap 与 getReqTokenString 的顺序
        PostRequest postRequest =
                OkGo.post(request.getUrl())
                        .tag(request.getTag())
                        .isMultipart(true)
                        .params(request.getRequestMap())
                        .headers("reqtoken", request.getReqTokenString());

        Map<String, File> files = request.getUploadFiles();

        if (files != null && files.size() != 0) {
            // 上传多个文件
            for (Map.Entry<String, File> entry : files.entrySet()) {
                File file = entry.getValue();
                if (file.exists()) {
                    String key = entry.getKey();
                    postRequest.params(key, file);
                }
            }
        }

        postRequest.execute(new BaseCallBack<>(request, responseClazz, netListener));
    }

    /**
     * 下载文件
     *
     * @param request 请求
     * @param dir 下载文件保存的目录
     * @param fileName 下载文件保存的文件名
     */
    public void downloadFile(
            BaseRequest request, String dir, String fileName, DownloadListener listener) {
        if (StringUtils.isEmpty(request.getUrl())) {
            throw new NullPointerException("Http Url can not be null !!!");
        } else if (StringUtils.isEmpty(request.getTag())) {
            throw new NullPointerException("Tag can not be null !!!");
        }

        // 所有接口加上app_version
        request.setAppVersion(SysUtils.getAppVersionName(UIUtils.getContext()));

        OkGo.get(request.getUrl())
                .tag(request.getTag())
                .headers("reqtoken", request.getReqTokenString())
                .execute(new BaseFileCallBack(request, dir, fileName, listener));
    }
}
